﻿@model Loja.Models.ProdutoModels
   
    <script type="text/javascript">
        function Validar() {
            return true;
        }

        function onSuccess(data) {
            if (data.success == true) {
                $("#ok").html(data.sucessMessage);
                $("#ok").fadeOut(5000);
                $("#lista").show();
                $("#Formulario").hide();
                $('#produtos').flexOptions({ url: '@Url.Content("~/Product/GetListaProduto")' }).flexReload();
            }
        }

        function onError() {
            alert("Erro");
        }

        function GetSubcategoria(idCategoria) {
            if (idCategoria > 0) {
                $.ajax({
                    url: '@Url.Content("~/Product/GetSubcategorias/0?idCategoria=")' + idCategoria,
                    type: "get",
                    success: function (data) {
                        var subcategorias = document.getElementById("IdCategoria");
                        subcategorias.options.length = 0;

                        for (i = 0; i < eval(data).length; i++)
                        {
                            subcategorias.options[subcategorias.options.length] = new Option(eval(data)[i].Text, eval(data)[i].Value);
                        }

//                        if (data) {
//                            $(data).each(function () {
//                                alert(this.Text);
//                                subcategorias.options[subcategorias.options.length] = new Option(this.Text, this.Value);
//                            });
//                        }
                    }

                });
            }
        }
 </script>   
      
      <div id="ok" style="display:none;"></div>
        
@using (Ajax.BeginForm(ViewData["Action"].ToString(), "Product", new AjaxOptions { HttpMethod = "POST", OnBegin = "Validar", OnSuccess = "onSuccess" }))
  {
    <div><input type="submit" value="Salvar" /></div>
    
    <br />
    
    <table width="100%">
        <tr>
            <td align="right" style="width: 150px">
                <label>
                    Categoria:</label>
            </td>
            <td>
                @Html.DropDownList("categorias", (List<SelectListItem>)ViewData["Categoria"], new { @class = "Selector", onChange="GetSubcategoria(this.value)" })
            </td>
        </tr>
        <tr>
            <td align="right">
                <label>
                    Subcategoria:</label>
            </td>
            <td>
                <select id="IdCategoria" name="IdCategoria"></select>
            </td>
        </tr>
        <tr>
            <td align="right">
                <label>
                   @Html.LabelFor(p => p.IdFornecedr)</label>
            </td>
            <td>
                @Html.DropDownListFor(p => p.IdFornecedr, (List<SelectListItem>)ViewData["Fornecedor"], new { @class = "Selector" })
            </td>
        </tr>
        <tr>
            <td align="right">
                <label>
                     @Html.LabelFor(p => p.IdMarca)</label>
            </td>
            <td>
                @Html.DropDownListFor(p => p.IdMarca, (List<SelectListItem>)ViewData["Marca"], new { @class = "Selector" })
            </td>
        </tr>
        <tr>
             <td align="right">
                <label>
                    @Html.LabelFor(p => p.Codigo)</label>
            </td>
            <td>
                @Html.TextBoxFor(p => p.Codigo)
            </td>
        </tr>
         <tr>
           <td align="right">
                <label>
                    @Html.LabelFor(p => p.Descricao)</label>
            </td>
            <td>
                @Html.TextAreaFor(p => p.Descricao)
            </td>
        </tr>
        <tr>
            <td align="right">
                <label>
                    @Html.LabelFor(p => p.PrecoCusto)</label>
            </td>
            <td>
                @Html.TextBoxFor(p => p.PrecoCusto)
            </td>
        </tr>
        <tr>
            <td align="right">
                <label>
                    @Html.LabelFor(p => p.Precentual)</label>
            </td>
            <td>
                @Html.TextAreaFor(p => p.Precentual)
            </td>
        </tr>
        <tr>
            <td align="right">
                <label>
                    @Html.LabelFor(p => p.Tags)</label>
            </td>
            <td>
               @Html.TextAreaFor(p => p.Tags)
            </td>
        </tr>
    </table>
  } 
